<template>
  <div class="login_container">
    <div class="login_box">
      <h1>Your Plan</h1>
      <el-form
        :model="loginForm"
        :rules="loginRules"
        ref="loginFormRef"
        :label-position="labelPosition"
        label-width="80px"
        class="login_form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" @keyup.enter.native="login"></el-input>
        </el-form-item>
        <div class="login-content">
          <el-checkbox v-model="loginForm.checked" class="form_checkBox">记住我</el-checkbox>
          <div class="login-btns">
            <el-button size="small " type="primary" @click="login">登录</el-button>
            <el-button size="small " @click="resetLoginForm">重 置</el-button>
            <el-button size="small " type="info" @click="dialogFormVisible = true">注册</el-button>
          </div>
        </div>
      </el-form>
    </div>

    <!--注册界面对话框-->
    <el-dialog
      title="注册"
      :visible.sync="dialogFormVisible"
      @closed="resetRegister"
    >
      <el-form
        :model="registerForm"
        ref="registerFormRef"
        :rules="registerRules"
        :label-position="labelPosition"
      >
        <el-form-item label="用户名" prop="username" :label-width="registerFormLabelWidth">
          <el-input v-model="registerForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass" :label-width="registerFormLabelWidth">
          <el-input type="password" v-model="registerForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass" :label-width="registerFormLabelWidth">
          <el-input type="password" v-model="registerForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email" :label-width="registerFormLabelWidth">
          <el-input v-model="registerForm.email" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button @click="resetRegistryForm">重 置</el-button>
        <el-button type="primary" @click="register">确 定</el-button>
      </div>
    </el-dialog>
    <!-- <canvas class="background"></canvas> -->
    <vue-particles
     color="#dedede"
    :particleOpacity="0.7"
    :particlesNumber="80"
    shapeType="circle"
    :particleSize="4"
    linesColor="#dedede"
    :linesWidth="1"
    :lineLinked="true"
    :lineOpacity="0.4"
    :linesDistance="150"
    :moveSpeed="3"
    :hoverEffect="true"
    hoverMode="grab"
    :clickEffect="true"
    clickMode="repulse"
    ></vue-particles>
  </div>
</template>

<script>
export default {
  data() {
    // 表单校验规则
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.registerForm.checkPass !== '') {
          this.$refs.registerFormRef.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.registerForm.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    var validateEmail = (rule, value, callback) => {
      var reg = new RegExp(
        '^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$'
      ) // 正则表达式
      if (value === '') {
        // 输入不能为空
        callback(new Error('请输入邮箱'))
        return false
      } else if (!reg.test(value)) {
        // 正则验证不通过，格式不对
        callback(new Error('请输入正确的邮箱'))
        return false
      } else {
        callback()
      }
    }

    return {
      loginForm: {
        username: '',
        password: '',
        checked: true
      },
      labelPosition: 'left',
      loginRules: {
        // 验证用户名是否合法
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在3到10个字符之间',
            trigger: 'blur'
          }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 3,
            max: 15,
            message: '长度在6到15个字符之间',
            trigger: 'blur'
          }
        ]
      },
      registerForm: {
        username: '',
        pass: '',
        checkPass: '',
        email: ''
      },
      registerFormLabelWidth: '120px',
      dialogFormVisible: false,
      dialogClickModal: false,
      registerRules: {
        // 验证用户名是否合法
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在3到10个字符之间',
            trigger: 'blur'
          }
        ],
        pass: [{ validator: validatePass, required: true, trigger: 'blur' }],
        checkPass: [
          { validator: validatePass2, required: true, trigger: 'blur' }
        ],
        email: [{ validator: validateEmail, required: true, trigger: 'blur' }]
      }
    }
  },
  created() {},
  mounted() {
  },
  methods: {
    // 点击重置按钮,重置登入表单
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },
    // 点击重置按钮,重置注册表单
    resetRegistryForm() {
      // console.log(this);
      this.$refs.registerFormRef.resetFields()
    },
    // 点击按钮进行预校验然后注册
    register() {
      console.log(this)
      this.$refs.registerFormRef.validate(
        async (valid) => {
          if (!valid) return
          const { data: res } = await this.$http.post('/user/register', this.registerForm)
          if (res.meta.status !== 200) {
            this.$message.error('注册失败！')
          }
          console.log(res.data)
          return this.$message.success('注册成功')
        }
      )
      this.dialogFormVisible = false
    },
    // 注册对话框关闭重置表单数据
    resetRegister() {
      this.$refs.registerFormRef.resetFields()
    },
    // 登入操作
    // 如果没有点击记住我设置一个定时任务,到时间之后直接取消token
    login() {
      this.$refs.loginFormRef.validate(
        async (valid) => {
          if (!valid) return
          const { data: res } = await this.$http.post('/user/login', this.loginForm)
          if (res.meta.status !== 200) {
            return this.$message.error('登录失败！用户名或者密码错误')
          }
          console.log(res.data)
          // 还需要将返回的token存储到浏览器中
          window.sessionStorage.setItem('token', res.data.token)
          this.$message.success('登录成功')
          // 进行一个编程式跳转
          this.$router.push('/home')
          if (this.checked === false) {
            setTimeout(() => {
              window.sessionStorage.clear()
            }, 1000 * 3600 * 6)
          } else {
            setTimeout(() => {
              window.sessionStorage.clear()
            }, 1000 * 3600 * 24 * 15)
          }
        }
      )
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  // background-color: #2b4b6b;
  height: 100%;
  overflow: hidden;
}

#particles-js{
  background-color: #2b4b6b;
  width: 100%;
  height: 100%;
}

.login_box {
  width: 400px;
  height: 300px;
  background-color: rgba(255, 255, 255, 1);
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 2%;
  z-index: 1;
  box-sizing: border-box;
}

.login_box h1 {
  text-align: center;
}

.login_form {
  width: 100%;
  position: absolute;
  bottom: 0px;
  padding: 0 20px;
  box-sizing: border-box;
}

.login-content{
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}
</style>
